package com.pro.system.Controller;

import com.pro.system.Repository.CommentRepository;
import com.pro.system.entity.Comment;
import com.pro.system.entity.Movie;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("admin/comments")
public class CommentController {

    @Autowired
    CommentRepository commentRepository;

    @GetMapping
    public String showComment (Model model){
        List<Comment> comment = commentRepository.findAll();
        model.addAttribute("comment",comment);
        return "web/background/comments/comments";
    }


    @GetMapping("intonew")
    public String intonew (Model model){
        Comment comment = new Comment();
        model.addAttribute(comment);
        return "web/background/comments/comments-new";
    }
    @GetMapping("intodelete")
    public String intodelete (Model model){
        Comment comment = new Comment();
        model.addAttribute(comment);
        return "web/background/comments/comments-delete";
    }

    @PostMapping("save")
    public String save(Model model,Comment comment) {
        Comment save = commentRepository.save(comment);
        if (save != null) {
            return "web/background/comments/comments-new";
        }
        else {
            return "";
        }
    }

    @PostMapping("delete")
    public String delete(Model model, Comment comment){
        Comment comment1 = new Comment();
        model.addAttribute(comment1);
        commentRepository.deleteById(comment.getCommentId());
        return "web/background/comments/comments-delete";
    }
}
